﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mime;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxCallbackPanel;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxClasses;
using DevExpress.XtraPrinting;

namespace Project_Udrinkwater.PagesAdmin
{
    public partial class Ordering : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string dateNow = ConvestToDate((DateTime.Now).ToShortDateString());
                Session["cStart"] = "0001-01-01";
                Session["cEnd"] = dateNow;
            }
            
        }

        protected void ASPxGridView2_BeforePerformDataSelect(object sender, EventArgs e)
        {
            Session["Order_ID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
        }

        protected void btnPdfExport_Click(object sender, EventArgs e)
        {
            gridExport.WritePdfToResponse();
        }
        protected void btnXlsExport_Click(object sender, EventArgs e)
        {
            gridExport.WriteXlsToResponse();
        }
        protected void btnXlsxExport_Click(object sender, EventArgs e)
        {
            gridExport.WriteXlsxToResponse();
        }
        protected void btnRtfExport_Click(object sender, EventArgs e)
        {
            gridExport.WriteRtfToResponse();
        }
        protected void btnCsvExport_Click(object sender, EventArgs e)
        {
            gridExport.WriteCsvToResponse();
        }

        protected void LinkButton1_Command(object sender, CommandEventArgs e)
        {
            string Order_ID = e.CommandArgument.ToString();
            Session["sOrder_ID"] = Order_ID;
            Response.Redirect("OrderList.aspx");
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            string a = ConvestToDateN((deStart.Date).ToShortDateString());
            string b = ConvestToDateN((edEnd.Date).ToShortDateString());
            if (a == "1-1-0001" && b == "1-1-0001")
            {
                a = " ทั้งหมด ";
                b = " ทั้งหมด ";
            }
            gridExport2.ReportHeader = "\t\t\tรายงานการขาย\n\t รายงานการขายตั้งแต่วันที่:" + a + "ถึงวันที่:" + b+"\n";
            gridExport2.WritePdfToResponse();

            //using (MemoryStream ms = new MemoryStream())
            //{
            //    PrintableComponentLink pcl = new PrintableComponentLink(new PrintingSystem());
            //    pcl.Component = gridExport2;
            //    pcl.CreateMarginalHeaderArea += new CreateAreaEventHandler(pcl_CreateMarginalHeaderArea);
            //    pcl.CreateMarginalFooterArea += new CreateAreaEventHandler(pcl_CreateMarginalFooterArea);
            //    pcl.Margins.Left = pcl.Margins.Right = 50;
            //    pcl.Landscape = false;
            //    pcl.CreateDocument(true);
            //    pcl.PrintingSystem.Document.AutoFitToPagesWidth = 1;
            //    pcl.ExportToPdf(ms);
            //    WriteResponse(Response, ms.ToArray(), System.Net.Mime.DispositionTypeNames.Attachment.ToString());
            //}
        }
        //void pcl_CreateMarginalFooterArea(object sender, CreateAreaEventArgs e)
        //{
        //    e.Graph.DrawBrick(new TextBrick() { Rect = new System.Drawing.RectangleF(0, 0, 200, 20), Text = "Test2" });

        //}
        //void pcl_CreateMarginalHeaderArea(object sender, CreateAreaEventArgs e)
        //{
        //    e.Graph.DrawBrick(new TextBrick() { Rect = new System.Drawing.RectangleF(0, 0, 200, 20), Text = "Test" });
        //}

        //public static void WriteResponse(HttpResponse response, byte[] filearray, string type)
        //{
        //    response.ClearContent();
        //    response.Buffer = true;
        //    response.Cache.SetCacheability(HttpCacheability.Private);
        //    response.ContentType = "application/pdf";
        //    ContentDisposition contentDisposition = new ContentDisposition();
        //    contentDisposition.FileName = "test.pdf";
        //    contentDisposition.DispositionType = type;
        //    response.AddHeader("Content-Disposition", contentDisposition.ToString());
        //    response.BinaryWrite(filearray);
        //    HttpContext.Current.ApplicationInstance.CompleteRequest();
        //    try
        //    {
        //        response.End();
        //    }
        //    catch (System.Threading.ThreadAbortException)
        //    {
        //    }

        //}
        protected void ASPxButton2_Click(object sender, EventArgs e)
        {
            string a = ConvestToDateN((deStart.Date).ToShortDateString());
            string b = ConvestToDateN((edEnd.Date).ToShortDateString());
            gridExport2.ReportHeader = "รายงานการขาย\n\t รายงานการขายตั้งแต่วันที่:" + a + "ถึงวันที่:" + b + "\n";
            gridExport2.WriteXlsToResponse();  
        }

        protected void ASPxButton3_Click(object sender, EventArgs e)
        {
            gridExport2.WriteXlsxToResponse();
        }

        protected void ASPxButton4_Click(object sender, EventArgs e)
        {
            gridExport2.WriteRtfToResponse();
        }

        protected void ASPxButton5_Click(object sender, EventArgs e)
        {
            gridExport2.WriteCsvToResponse();
        }

        protected void ASPxButton6_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel3.Visible = true;
            Panel2.Visible = true;
            Panel4.Visible = true;
            grid.Visible = true;
        }
        public string ConvestToDateN(string s)
        {
            string day = "";
            string mount = "";
            string years = "";

            try
            {
                string[] stemp = s.Split('/');
                mount = (stemp[0]);
                day = (stemp[1]);
                years = (stemp[2]);
                if (Convert.ToInt32(years) > 2300)
                {
                    years = (Convert.ToInt32(years) - 543).ToString();
                }

                return (day + "-" + mount + "-" + years);
            }
            catch (Exception)
            {

            }
            return (day + "-" + mount + "-" + years);
        }
        public string ConvestToDate(string s)
        {
            string day = "" ;
            string mount ="";
            string years ="" ;
            
            try
            {
                string[] stemp = s.Split('/');
                mount = (stemp[0]);
                day = (stemp[1]);
                years = (stemp[2]);
                if (Convert.ToInt32(years) > 2300)
                {
                    years = (Convert.ToInt32(years) - 543).ToString();
                }
                
                return (years+"-"+mount+"-"+day);
            }
            catch (Exception)
            {

            }
            return (years + "-" + mount + "-" + day);
        }
        protected void ASPxButton7_Click(object sender, EventArgs e)
        {
            string a = ConvestToDate((deStart.Date).ToShortDateString());
            string b = ConvestToDate((edEnd.Date).ToShortDateString());
            Session["cStart"] = a;
            Session["cEnd"] = b;
            SqlOderListSert.Select(DataSourceSelectArguments.Empty);
            grid.DataBind();
        }

        protected void SqlOderListSert_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@sStart"].Value = Session["cStart"].ToString();
            e.Command.Parameters["@sEnd"].Value = Session["cEnd"].ToString();
        }

        protected void ASPxButton8_Click(object sender, EventArgs e)
        {
            string a = ConvestToDate((deStart0.Date).ToShortDateString());
            string b = ConvestToDate((edEnd0.Date).ToShortDateString());
            Session["cStart"] = a;
            Session["cEnd"] = b;
            SqlOrder.Select(DataSourceSelectArguments.Empty);
            ASPxGridView1.DataBind();
        }

        protected void SqlOrder_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@sStart"].Value = Session["cStart"].ToString();
            e.Command.Parameters["@sEnd"].Value = Session["cEnd"].ToString();
        }

        protected void ASPxButton9_Click(object sender, EventArgs e)
        {
            string dateNow = ConvestToDate((DateTime.Now).ToShortDateString());
            Session["cStart"] = "0001-01-01";
            Session["cEnd"] = dateNow;
            SqlOderListSert.Select(DataSourceSelectArguments.Empty);
            grid.DataBind();
        }

        protected void btShowAllOrder_Click(object sender, EventArgs e)
        {
            string dateNow = ConvestToDate((DateTime.Now).ToShortDateString());
            Session["cStart"] = "0001-01-01";
            Session["cEnd"] = dateNow;
            SqlOrder.Select(DataSourceSelectArguments.Empty);
            ASPxGridView1.DataBind();
        }

        protected void btback_Click(object sender, EventArgs e)
        {
           
        }

        //protected void CmbCity_Callback(object source, CallbackEventArgsBase e)
        //{
        //    FillCityCombo(e.Parameter);
        //}

        //protected void FillCityCombo(string country)
        //{
        //    if (string.IsNullOrEmpty(country)) return;

        //    AccessDataSourceCities.SelectParameters[0].DefaultValue = country;
        //    CmbCity.DataBind();


        //    // Select the current country capital in the CmbCity
        //    DataTable table = ((DataView) AccessDataSourceCountry.Select(DataSourceSelectArguments.Empty)).Table;
        //    DataRow[] foundRows = table.Select(string.Format("Product_Name = '{0}'", country));
        //    if (foundRows.Length > 0)
        //        CmbCity.Value = (string) foundRows[0]["Product_ID"];  
        //}



        //protected void btOK_Click(object sender, EventArgs e)
        //{

        //    if (CmbCountry.SelectedItem == null)
        //    {
        //        Label2.Visible = true;
        //        CmbCountry.Focus();
        //    }
            
            
        //    else if (CmbCity.SelectedItem == null)
        //    {
        //        CmbCity.Focus();
        //        Label3.Visible = true;

        //    }
        //    else if (txtAmonut.Text == "")
        //    {
        //        Label1.Visible = true;
        //        txtAmonut.Focus();
        //    }
        //    else
        //    {   
        //        if (txtDiscount.Text == "")
        //        {
        //            txtDiscount.Text = "0";
        //        }
        //        SqlOderList.InsertParameters["Order_ID"].DefaultValue = Session["Order_ID"].ToString();
        //        SqlOderList.InsertParameters["Product_ID"].DefaultValue = CmbCountry.Value.ToString();
        //        SqlOderList.InsertParameters["Order_Category_ID"].DefaultValue = CmbCity.Value.ToString();
        //        string a = CmbCity.SelectedItem.Text;
        //        string[] stemp = a.Split(':');
        //        int Price = Convert.ToInt32(stemp[1]);
        //        SqlOderList.InsertParameters["Price_Unit"].DefaultValue = Price.ToString();
        //        SqlOderList.InsertParameters["Amount"].DefaultValue = txtAmonut.Text;
        //        SqlOderList.InsertParameters["Discount"].DefaultValue = txtDiscount.Text;
        //        SqlOderList.Insert();
        //        ASPxGridView1.DataBind();
        //        CmbCountry.SelectedItem = null;
        //        CmbCity.SelectedItem = null;
        //        txtAmonut.Text = "";
        //        txtDiscount.Text = "";
        //        Label1.Visible = false;
        //        Label2.Visible = false;
        //        Label3.Visible = false;

        //    }
            
            
            
            
        //}

        //protected void btCancel_Click(object sender, EventArgs e)
        //{
           
        //    CmbCountry.SelectedItem = null;
        //    CmbCity.SelectedItem = null;
        //    txtAmonut.Text = "";
        //    txtDiscount.Text = "";
        //    Label1.Visible = false;
        //    Label2.Visible = false;
        //    Label3.Visible = false;
        //}

        
    }
}